package com.atguigu.gmall.all.controller;

import com.atguigu.gmall.acitvity.client.ActivityFeignClient;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.util.AuthContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Author: zm
 * @Date: 2021/8/31 20:50
 */
@Controller
public class SeckillController {

    @Autowired
    private ActivityFeignClient activityFeignClient;

    // http://activity.gmall.com/seckill.html
    @GetMapping("seckill.html")
    public String toSeckillPage(Model model) {

        Result all = activityFeignClient.findAll();
        model.addAttribute("list", all.getData());

        return "seckill/index";
    }

    // /seckill/'+${item.skuId}+'.html'"

    @GetMapping("seckill/{skuId}.html")
    public String b(@PathVariable Long skuId, Model model) {
        Result seckillGoods = activityFeignClient.getSeckillGoods(skuId);
        model.addAttribute("item", seckillGoods.getData());
        return "seckill/item";
    }

    // '/seckill/queue.html?skuId='+this.skuId+'&skuIdStr='+skuIdStr
    @GetMapping("seckill/queue.html")
    public String toQueue(HttpServletRequest request, Model model) {

        String skuId = request.getParameter("skuId");
        String skuIdStr = request.getParameter("skuIdStr");

        request.setAttribute("skuId", skuId);
        request.setAttribute("skuIdStr", skuIdStr);

        return "seckill/queue";
    }

    // /seckill/trade.html
    @GetMapping("/seckill/trade.html")
    public String toTrade(Model model) {
        // userAddressList  detailArrayList  totalNum  totalAmount
        Result<Map<String, Object>> result = activityFeignClient.trade();
        if (result.isOk()) {
            // OK
            model.addAllAttributes(result.getData());
            return "seckill/trade";
        } else {
            //  不OK!
            model.addAttribute("message", result.getMessage());
            return "seckill/fail";
        }


    }

}
